package com.mi.service.impl;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mi.bean.GoodsBean;
import com.mi.bean.ShoppingcartBean;
import com.mi.db.DBProvider;
import com.mi.service.IAddCartService;
import com.mi.utils.MyUtils;

public class AddGoodsService implements IAddCartService {

	/**
	 * 根据商品信息和用户id确定往购物车内添加的商品信息
	 * 
	 * @param Goods   商品信息
	 * @param customer_id 用户id
	 * @return 是否加入成功
	 */
	@Override
	public int addGod2Shopcart(GoodsBean goods, String customer_id) {
		int i = 0;
		
		// 根据商品id和用户id确定往购物车内添加的商品信息
		// 使用子查询，往购物车表中插入用户信息商品信息
		String sql = "insert into shoppingcart (customer_id,goods_id,goods_name,goods_price,counts,goods_img)\r\n" + 
				"            values(?,?,?,?,?,?)";
		
		i = MyUtils.getInstance(DBProvider.class).update(sql, customer_id, goods.getGoods_id(),goods.getGoods_name(),
				goods.getGoods_price(),goods.getGoods_price(),goods.getGoods_img());
		
		return i;
	}

	/**
	 * 根据商品id查询商品信息
	 * 
	 * @param goods_id 商品id
	 * @return 商品信息实体类
	 */
	@Override
	public List<GoodsBean> queryGoodsInfo(int goods_id) {

		String sql = "select * from goods where goods_id = ?";

		// ResultSetHandler接口de实现类--BeanListHandler把结果集中的每行数据都封装成一个指定的JavaBean对象
		// 将数据封装成list<JavaBean>形式
		List<GoodsBean> lstGoodsInfo = MyUtils.getInstance(DBProvider.class).query(sql,
				new BeanListHandler<GoodsBean>(GoodsBean.class),goods_id);
		return lstGoodsInfo;
	}

	/**
	 * 根据用户id查询该用户的购物车信息
	 * @param customer_id 用户id
	 * @return	购物车信息实体类
	 */
	@Override
	public List<ShoppingcartBean> queryShopInfo(String customer_id) {
		
		String sql = "select * from shoppingcart where customer_id = ?";

		// ResultSetHandler接口de实现类--BeanListHandler把结果集中的每行数据都封装成一个指定的JavaBean对象
		// 将数据封装成list<JavaBean>形式
		List<ShoppingcartBean> lstShopcartInfo = MyUtils.getInstance(DBProvider.class).query(sql,
				new BeanListHandler<ShoppingcartBean>(ShoppingcartBean.class),customer_id);
		return lstShopcartInfo;
	}

}
